package viewcontrollers;

import pojos.TimeLog;
import viewcontrollers.util.JsfUtil;
import viewcontrollers.util.PaginationHelper;
import beans.TimeLogFacade;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpSession;
import pojos.Employees;
import pojos.TimeLogViewBean;
import util.CurrentTimeToGMT;
import util.SendMail;
import util.TimeSheetExcelFile;
import viewcontrollers.util.XMLParser;

@ManagedBean(name = "timeLogController")
@SessionScoped
public class TimeLogController implements Serializable {

    private TimeLog current;
    private DataModel items = null;
    private DataModel itemsSheet = null;
    private DataModel userItems = null;
    private DataModel nestedItems = null;
    @EJB
    private beans.TimeLogFacade ejbFacade;
    private PaginationHelper pagination;
    private PaginationHelper paginationSheet;
    private PaginationHelper userPagination;
    private PaginationHelper nestedPagination;
    private int selectedItemIndex;
    private Date workingDay;
    private Date weekDay;
    private double workingHour;
    private double overtime;
    private List activities;
    private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();

    public TimeLogController() {
        String is = this.getClass().getClassLoader().getResource("activities.xml").getPath();
        XMLParser parser = new XMLParser();
        parser.parseActivities(is);
        List<String> activ = parser.getActivities();
        this.setActivities(activ);
    }

    public List getActivities() {
        return activities;
    }

    public void setActivities(List activities) {
        this.activities = activities;
    }

    public Date getWeekDay() {
        return weekDay;
    }

    public void setWeekDay(Date weekDay) {
        this.weekDay = weekDay;
    }

    public TimeLog getSelected() {
        if (current == null) {
            current = new TimeLog();
            selectedItemIndex = -1;
        }
        return current;
    }

    private TimeLogFacade getFacade() {
        return ejbFacade;
    }

    public PaginationHelper getPagination() {
        if (pagination == null) {
            pagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return pagination;
    }

    public PaginationHelper getUserPagination() {
        if (userPagination == null) {
            userPagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                    String userMail = (String) session.getValue("userName");
                    Employees employee = getFacade().searchEmployee(userMail);
                    return new ListDataModel(getFacade().getUserTimeLog(employee));
                }
            };
        }
        return userPagination;
    }

    public String prepareList() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        recreateModel();
        if (userType.equalsIgnoreCase("admin")) {
            return "List";
        } else {
            return "userList";
        }
    }

    public String prepareView(String tlId) {
        int tlID = Integer.parseInt(tlId);
        try {
            current = (TimeLog) getItems().getRowData();
        } catch (ClassCastException e) {
            TimeLogViewBean tlvb = (TimeLogViewBean) getItems().getRowData();
            List<TimeLog> li = tlvb.getTimlog();
            for (int i = 0; i < li.size(); i++) {
                TimeLog tl = li.get(i);
                if (tl.getTlId() == tlID) {
                    current = tl;
                }
            }
        }
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        if (userType.equalsIgnoreCase("admin")) {
            selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        } else {
            selectedItemIndex = userPagination.getPageFirstItem() + getUserItems().getRowIndex();
        }
        return "View";
    }

    public String prepareEdit(String tlId) {
        int tlID = Integer.parseInt(tlId);
        try {
            current = (TimeLog) getItems().getRowData();
        } catch (ClassCastException e) {
            TimeLogViewBean tlvb = (TimeLogViewBean) getItems().getRowData();
            List<TimeLog> li = tlvb.getTimlog();
            for (int i = 0; i < li.size(); i++) {
                TimeLog tl = li.get(i);
                if (tl.getTlId() == tlID) {
                    current = tl;
                }
            }
        }
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        if (userType.equalsIgnoreCase("admin")) {
            selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        } else {
            selectedItemIndex = userPagination.getPageFirstItem() + getUserItems().getRowIndex();
        }
        return "Edit";
    }

    public String prepareCreate() {
        current = new TimeLog();
        selectedItemIndex = -1;
        return "Create";
    }

    public String create() {
        try {
            CurrentTimeToGMT gmtTime = new CurrentTimeToGMT();
            current.setCreatedAt(gmtTime.getGMT());

            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            String userMail = (String) session.getValue("userName");
            String userType = (String) session.getValue("type");
            Employees employee = getFacade().searchEmployee(userMail);
            current.setCreatedBy(employee);
            if (userType.equalsIgnoreCase("user")) {
                current.setEmplyee(employee);
            }
            current.setStatus("saved");
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogCreated"));
            return prepareCreate();
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String createAndList() {
        try {
            CurrentTimeToGMT gmtTime = new CurrentTimeToGMT();
            current.setCreatedAt(gmtTime.getGMT());

            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            String userMail = (String) session.getValue("userName");
            String userType = (String) session.getValue("type");
            Employees employee = getFacade().searchEmployee(userMail);
            current.setCreatedBy(employee);
            if (userType.equalsIgnoreCase("user")) {
                current.setEmplyee(employee);
            }
            current.setStatus("saved");
            getFacade().create(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogCreated"));
            return prepareList();
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String update() {
        try {
            CurrentTimeToGMT gmtTime = new CurrentTimeToGMT();
            current.setCreatedAt(gmtTime.getGMT());

            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
            String userMail = (String) session.getValue("userName");
            String userType = (String) session.getValue("type");
            Employees employee = getFacade().searchEmployee(userMail);
            if (userType.equalsIgnoreCase("user")) {
                current.setEmplyee(employee);
            }
            current.setCreatedBy(employee);
            getFacade().edit(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        try {
            current = (TimeLog) getItems().getRowData();
        } catch (ClassCastException e) {
            TimeLogViewBean tlvb = (TimeLogViewBean) getItems().getRowData();
            current = tlvb.getTimlog().get(0);
        }
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        if (userType.equalsIgnoreCase("admin")) {
            selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
        } else {
            selectedItemIndex = userPagination.getPageFirstItem() + getItems().getRowIndex();
        }
        performDestroy();
        recreateModel();
        if (userType.equalsIgnoreCase("admin")) {
            return "List";
        } else {
            return "userList";
        }

    }

    public String destroyAndView() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        performDestroy();
        recreateModel();
        updateCurrentItem();
        if (selectedItemIndex >= 0) {
            return "View";
        } else {
            // all items were removed - go back to list
            recreateModel();
            if (userType.equalsIgnoreCase("admin")) {
                return "List";
            } else {
                return "userList";
            }

        }
    }

    private void performDestroy() {
        try {
            getFacade().remove(current);
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogDeleted"));
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    private void updateCurrentItem() {
        int count = getFacade().count();
        if (selectedItemIndex >= count) {
            // selected index cannot be bigger than number of items:
            selectedItemIndex = count - 1;
            // go to previous page if last page disappeared:
            if (pagination.getPageFirstItem() >= count) {
                pagination.previousPage();
            }
        }
        if (selectedItemIndex >= 0) {
            current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
        }
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPagination().createPageDataModel();
        }
        return items;
    }

    public DataModel getUserItems() {
        if (userItems == null) {
            userItems = getUserPagination().createPageDataModel();
            items = userItems;
        }
        return items;
    }

    private void recreateModel() {
        items = null;
        userItems = null;
        itemsSheet = null;
        nestedItems = null;
    }

    public String next() {
        getPagination().nextPage();
        recreateModel();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        if (userType.equalsIgnoreCase("admin")) {
            return "List";
        } else {
            return "userList";
        }
    }

    public String previous() {
        getPagination().previousPage();
        recreateModel();
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        if (userType.equalsIgnoreCase("admin")) {
            return "List";
        } else {
            return "userList";
        }
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
    }

    @FacesConverter(forClass = TimeLog.class)
    public static class TimeLogControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            TimeLogController controller = (TimeLogController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "timeLogController");
            return controller.ejbFacade.find(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof TimeLog) {
                TimeLog o = (TimeLog) object;
                return getStringKey(o.getTlId());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + TimeLogController.class.getName());
            }
        }
    }

    public DataModel getItemsSheet() {
        itemsSheet = null;
        if (workingDay != null) {
            itemsSheet = getPaginationSheet().createPageDataModel();
        }
        items = itemsSheet;
        return itemsSheet;
    }

    public PaginationHelper getPaginationSheet() {
        if (paginationSheet == null) {
            paginationSheet = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    // 2 this number should be replaced with employee ID 
                    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                    String userName = session.getAttribute("userName").toString();
                    String userPass = session.getAttribute("password").toString();
                    String empId = session.getAttribute("empid").toString();
                    int empID = Integer.parseInt(empId);
                    List week = getFacade().getProjects(empID, workingDay);

                    /// reset working hour and overtime 
                    workingHour = 0;
                    overtime = 0;

                    Iterator<TimeLog> itr = week.iterator();
                    while (itr.hasNext()) {
                        TimeLog dayworked = itr.next();
                        workingHour += dayworked.getWorkedHours();
                        if (dayworked.getWorkedHours() - 8 > 0) // if working hour more than 8 .....add to overtime 
                        {
                            overtime += (dayworked.getWorkedHours() - 8);
                        }
                    }
                    return new ListDataModel(week);
                }
            };
        }
        return paginationSheet;
    }

    public Date getWorkingDay() {
        return workingDay;
    }

    public void setWorkingDay(Date workingDay) {
        this.workingDay = workingDay;
    }

    public double getOvertime() {
        return overtime;
    }

    public double getWorkingHour() {
        return workingHour - overtime;
    }

    public String empInfo() {
        return "EmployeeTimeSheet";
    }

    public Map<Long, Boolean> getChecked() {
        return checked;
    }

    public void setChecked(Map<Long, Boolean> checked) {
        this.checked = checked;
    }

    /**
     * deleteNestedRows
     * @return 
     */
    public String deleteSelectedNested() {
        try {
            List<TimeLog> checkedTimeLogs = new ArrayList<TimeLog>();
            Iterator<TimeLogViewBean> itr = items.iterator();
            while (itr.hasNext()) {
                TimeLogViewBean timelogviewbean = itr.next();
                for (int co = 0; co < timelogviewbean.getTimlog().size(); co++) {
                    TimeLog timelog = timelogviewbean.getTimlog().get(co);
                    if (checked.get(timelog.getTlId())) {
                        if (!timelog.getStatus().equalsIgnoreCase("submitted")) {
                            checkedTimeLogs.add(timelog);
                        } else {
                            JsfUtil.addErrorMessage(ResourceBundle.getBundle("/Bundle").getString("DeleteError"));
                            recreateModel();
                            return "nestedtest";
                        }
                    }
                }
            }
            getFacade().deleteTimeLog(checkedTimeLogs);
            checked.clear();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogsDeleted"));
            recreateModel();
            return "nestedtest";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("TimeLogsDeleteError"));
            recreateModel();
            return "nestedtest";
        }
    }

    /**
     * Submit Selected Nested time Logs
     * @return 
     */
    public String submitNested() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userName = session.getAttribute("userName").toString();
        String userPass = session.getAttribute("password").toString();
        try {
            List<TimeLog> checkedTimeLogs = new ArrayList<TimeLog>();
            Iterator<TimeLogViewBean> itr = items.iterator();
            while (itr.hasNext()) {
                TimeLogViewBean timelogviewbean = itr.next();
                for (int co = 0; co < timelogviewbean.getTimlog().size(); co++) {
                    TimeLog timelog = timelogviewbean.getTimlog().get(co);
                    if (checked.get(timelog.getTlId())) {
                        if (timelog.getStatus().equalsIgnoreCase("submitted")) {
                            JsfUtil.addErrorMessage("There are Some TimeLogs Submitted Before, So They can not be Submitted Again.");
                            recreateModel();
                            return "nestedtest";
                        } else {
                            timelog.setStatus("submitted");
                            getFacade().edit(timelog);
                            checkedTimeLogs.add(timelog);
                        }
                    }
                }
            }
            String fileName = "TimeLogs_" + userName + "_" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".xls";
            String filePath = new TimeSheetExcelFile().generateExcelFile(fileName, checkedTimeLogs);

            SendMail sm = new SendMail();
            sm.sendMailWithAttachFile(fileName, userName, filePath, userPass);
            
            checked.clear();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogsSubmitted"));
            recreateModel();
            return "nestedtest";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("TimeLogsSubmitError"));
            recreateModel();
            return "nestedtest";
        }
    }

    /**
     * Delete Selected Time Logs 
     * @return 
     */
    public String deleteSelected() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        try {
            List<TimeLog> checkedTimeLogs = new ArrayList<TimeLog>();
            Iterator<TimeLog> itr = items.iterator();
            while (itr.hasNext()) {
                TimeLog timelog = itr.next();
                if (checked.get(timelog.getTlId())) {
                    if (!timelog.getStatus().equalsIgnoreCase("submitted")) {
                        checkedTimeLogs.add(timelog);
                    } else {
                        JsfUtil.addErrorMessage(ResourceBundle.getBundle("/Bundle").getString("DeleteError"));
                        if (userType.equalsIgnoreCase("admin")) {
                            return prepareList();
                        } else {
                            return prepareList();
                        }
                    }
                }
            }
            getFacade().deleteTimeLog(checkedTimeLogs);
            checked.clear();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogsDeleted"));
            recreateModel();
            if (userType.equalsIgnoreCase("admin")) {
                return "list";
            } else {
                return "userList";
            }
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("TimeLogsDeleteError"));
            recreateModel();
            if (userType.equalsIgnoreCase("admin")) {
                return "list";
            } else {
                return "userList";
            }
        }
    }

    /**
     * Save Selected Time Logs
     * @return 
     */
    public String saveSelected() {
        try {
            List<TimeLog> checkedTimeLogs = new ArrayList<TimeLog>();
            Iterator<TimeLog> itr = items.iterator();
            while (itr.hasNext()) {
                TimeLog timelog = itr.next();
                if (checked.get(timelog.getTlId())) {
                    timelog.setStatus("saved");
                    checkedTimeLogs.add(timelog);
                    getFacade().edit(timelog);
                }
            }
            checked.clear();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogSaved"));
            recreateModel();
            return "List";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("TimeLogSaveError"));
            return "List";
        }
    }

    /**
     * Submit Selected time Logs
     * @return 
     */
    public String submit() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userType = (String) session.getValue("type");
        String userName = session.getAttribute("userName").toString();
        String userPass = session.getAttribute("password").toString();
        try {
            List<TimeLog> checkedTimeLogs = new ArrayList<TimeLog>();
            Iterator<TimeLog> itr = items.iterator();
            while (itr.hasNext()) {
                TimeLog timelog = itr.next();
                if (checked.get(timelog.getTlId())) {
                    if (timelog.getStatus().equalsIgnoreCase("submitted")) {
                        JsfUtil.addErrorMessage("There are Some TimeLogs Submitted Before, So They can not be Submitted Again.");
                        recreateModel();
                        if (userType.equalsIgnoreCase("admin")) {
                            return "List";
                        } else {
                            return "userList";
                        }
                    } else {
                        timelog.setStatus("submitted");
                        getFacade().edit(timelog);
                        checkedTimeLogs.add(timelog);
                    }
                }
            }
            String fileName = "TimeLogs_" + userName + "_" + new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + ".xls";
            String filePath = new TimeSheetExcelFile().generateExcelFile(fileName, checkedTimeLogs);

            SendMail sm = new SendMail();
            sm.sendMailWithAttachFile(fileName, userName, filePath, userPass);

            checked.clear();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("TimeLogsSubmitted"));
            recreateModel();
            if (userType.equalsIgnoreCase("admin")) {
                return "List";
            } else {
                return "userList";
            }
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("TimeLogsSubmitError"));
            recreateModel();
            if (userType.equalsIgnoreCase("admin")) {
                return "List";
            } else {
                return "userList";
            }
        }
    }

    public String listUserTimeLog() {
        return "/timeLog/userList";
    }

    public String nestedTables() {
        return "nestedtest";
    }

    public String displayWeekTimeLog() {
        System.out.println("##############Date: " + weekDay);
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        String userMail = (String) session.getValue("userName");
        Employees employee = getFacade().searchEmployee(userMail);
        System.out.println("################Employee Business Mail: " + employee.getBusinessEmail());
        return "nestedtest";
    }

    public PaginationHelper getNestedPagination() {
        System.out.println("##################inside getNestedPagination");
        if (nestedPagination == null) {
            nestedPagination = new PaginationHelper(10) {

                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
                    String userMail = (String) session.getValue("userName");
                    Employees employee = getFacade().searchEmployee(userMail);
                    List weektest = getFacade().getProjects(employee.getEmpId(), weekDay);
                    System.out.println("//////////////////////////Week Test: " + weektest.size() + "//////////////");
                    List week = getFacade().getNestedTimeLog(weekDay, employee);
                    /// reset working hour and overtime 
                    workingHour = 0;
                    overtime = 0;

                    Iterator<TimeLogViewBean> itr = week.iterator();
                    while (itr.hasNext()) {
                        List<TimeLog> dayworked = itr.next().getTimlog();
                        for (int i = 0; i < dayworked.size(); i++) {
                            workingHour += dayworked.get(i).getWorkedHours();
                            if (dayworked.get(i).getWorkedHours() - 8 > 0) // if working hour more than 8 .....add to overtime 
                            {
                                overtime += (dayworked.get(i).getWorkedHours() - 8);
                            }
                        }
                        session.putValue("weekDaysHours", workingHour);
                        session.putValue("overTimeHours", overtime);
                        session.putValue("totalWorkedDays", weektest.size());
                    }
                    return new ListDataModel(week);
                }
            };
        }
        return nestedPagination;
    }

    public DataModel getNestedItems() {
        System.out.println("###############inside getNestedItems#################");
        nestedItems = null;
        if (weekDay != null) {
            nestedItems = getNestedPagination().createPageDataModel();
        }
        items = nestedItems;
        return items;
    }
}
